package com.csun.cmny.provider.controller;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.levelCareDataMigration.BedOfficialVo;
import com.csun.cmny.provider.levelCareDataMigration.RoomOfficialVo;
import com.csun.cmny.provider.model.domain.OmcLevelCare;
import com.csun.cmny.provider.model.vo.VolunteerVo;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/levelCareDataMigration", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class levelCareDataMigrationController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private BedService bedService;

    @Resource
    private RoomService roomService;

    @Resource
    private OmcLevelCareService levelCareService;

    @Resource
    private levelCareDataMigrationService dataMigrationService;

    @Resource
    private CacRpcService cacRpcService;
    @Resource
    private UacRpcService uacRpcService;

    //床位表数据更正
 /*   @RequestMapping(value = "/dataMigrationToBed", method = RequestMethod.POST)
    public Wrapper queryInExpenseItem(){

        Integer count=0;

        List<BedOfficialVo> bedVos=dataMigrationService.selectbedVos();

        for (BedOfficialVo bedVo : bedVos) {
            if (PublicUtil.isNotEmpty(bedVo.getBedType())&&PublicUtil.isNotEmpty(bedVo.getHomeId())&&PublicUtil.isEmpty(bedVo.getLevelCareId())){
                OmcLevelCare omcLevelCare = new OmcLevelCare();
                omcLevelCare.setGroupId(bedVo.getHomeId().longValue());
                if (bedVo.getBedType()==1){
                    omcLevelCare.setNursingCare("自理");
                }else if (bedVo.getBedType()==2){
                    omcLevelCare.setNursingCare("半自理");
                }else if (bedVo.getBedType()==3){
                    omcLevelCare.setNursingCare("失能");
                }
                try {
                    omcLevelCare = levelCareService.selectOne(omcLevelCare);
                }catch (Exception e){
                    throw new OmcBizException(ErrorCodeEnum.OMC10032048,bedVo.getBedId());
                }

                Bed bed = new Bed();
                bed.setBedId(bedVo.getBedId());
                bed.setLevelCareId(omcLevelCare.getId());
                count+=bedService.updateById(bed);
            }

        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),count);
    }

    //房间表数据更正
    @RequestMapping(value = "/dataMigrationToRoom", method = RequestMethod.POST)
    public Wrapper dataMigrationToRoom(){

        Integer count=0;

        List<RoomOfficialVo> roomOfficialVos=dataMigrationService.selectroomOfficialVos();

        for (RoomOfficialVo roomOfficialVo : roomOfficialVos) {

            if (PublicUtil.isNotEmpty(roomOfficialVo.getBedType())&&PublicUtil.isNotEmpty(roomOfficialVo.getHomeId())&&PublicUtil.isEmpty(roomOfficialVo.getLevelCareId())){
                OmcLevelCare omcLevelCare = new OmcLevelCare();
                omcLevelCare.setGroupId(roomOfficialVo.getHomeId().longValue());
                if (roomOfficialVo.getBedType()==1){
                    omcLevelCare.setNursingCare("自理");
                }else if (roomOfficialVo.getBedType()==2){
                    omcLevelCare.setNursingCare("半自理");
                }else if (roomOfficialVo.getBedType()==3){
                    omcLevelCare.setNursingCare("失能");
                }
                try {
                    omcLevelCare = levelCareService.selectOne(omcLevelCare);
                }catch (Exception e){
                    throw new OmcBizException(ErrorCodeEnum.OMC10032048,roomOfficialVo.getRoomId());
                }

                Room room = new Room();
                room.setRoomId(roomOfficialVo.getRoomId());
                room.setLevelCareId(omcLevelCare.getId());
                count+=roomService.updateById(room);
            }
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),count);
    }


    //老人评估表数据更正
    @RequestMapping(value = "/dataMigrationToCacOldMan", method = RequestMethod.POST)
    public Wrapper dataMigrationToCacOldMan(){

        List<Map<String,Object>>cacOldMans=cacRpcService.selectCacOldManAll();

        List<Map<String,Object>>updateDtos=new ArrayList<>();

        for (Map<String, Object> cacOldMan : cacOldMans) {
            if (PublicUtil.isNotEmpty(cacOldMan.get("finalGrade"))){
                OmcLevelCare omcLevelCare = new OmcLevelCare();
                Integer homeId=(Integer) cacOldMan.get("homeId");
                omcLevelCare.setGroupId(homeId.longValue());
                Integer ob = (Integer) cacOldMan.get("finalGrade");
                if (ob==0){
                    omcLevelCare.setNursingCare("自理");
                }else if (ob==1||ob==2){
                    omcLevelCare.setNursingCare("半自理");
                }else if (ob==3){
                    omcLevelCare.setNursingCare("失能");
                }
                if (PublicUtil.isNotEmpty(omcLevelCare.getNursingCare())){
                    try {
                        omcLevelCare = levelCareService.selectOne(omcLevelCare);
                    }catch (Exception e){
                        System.out.println("护理等级"+omcLevelCare);
                        throw new OmcBizException(ErrorCodeEnum.OMC10032048,cacOldMan.get("cacId"));
                    }
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("cacId",cacOldMan.get("cacId"));
                    map.put("levelCareId",omcLevelCare.getId());
                    map.put("levelCareName",omcLevelCare.getNursingCare());

                    updateDtos.add(map);
                }
            }
        }

        Integer update=cacRpcService.updateLevelCare(updateDtos);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),update);
    }*/

   /* @RequestMapping(value = "/chisj", method = RequestMethod.POST)
    public Wrapper chisj(){

        List<Map<String,Object>> updateDtos=new ArrayList<>();

        HashMap<String, Object> map = new HashMap<>();
        map.put("cacId", 99999l);
        map.put("level_care_id", 99999l);
        map.put("level_care_name", "12345");

        updateDtos.add(map);

         Integer update = cacRpcService.updateLevelCare(updateDtos);

//        cacRpcService.test(updateDtos);

//        List<VolunteerVo> volunteerVoList = uacRpcService.queryVolunteerListByVolunteerIds(null);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), null);
    }*/
}
